// pages/goods/receive/receive.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderinfo: {},
    goodsinfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取订单详情
    this.getOrderDetail(options.order_id);
  },

  //换星星
  exchangeStars: function () {
    var self = this;
    wx.showModal({
      title: '提示',
      content: '是否确定将抓中的商品兑换成星星',
      success: function (res) {
        if (res.confirm) {
          app.Ajax({
            method: 'POST',
            data: { order_id: self.data.orderinfo.order_id },
            url: '/api/index.php?app=account&act=exchange',
            success: (res) => {
              self.toastShow('兑换成功');
              //返回上一级页面
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            },
            fail: (err) => {
              self.toastShow(err);
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  //请求发货
  requestShipping: function () {
    let params = '?goods_id=' + this.data.goodsinfo.goods_id + '&goods_num=' + this.data.goodsinfo.quantity + '&goods_type=' + this.data.orderinfo.type + '&sku_id=' + this.data.goodsinfo.sku_id + '&order_id=' + this.data.orderinfo.order_id;
    // console.log(params);
    wx.redirectTo({
      url: '../../order/goods_order/goods_order' + params,
    })
  },

  getOrderDetail: function (targetId) {
    app.Ajax({
      url: '/api/index.php?app=order&act=detail',
      data: {
        order_id: targetId
      },
      success: (res) => {
        // console.log(res);
        this.setData({
          orderinfo: res,
          goodsinfo: res.goods[0],
        });
        let barTitle = '';
        switch (res.status) {
          case 10: barTitle = '待领取'; break;
          default: barTitle = '已领取'; break;
        }
        wx.setNavigationBarTitle({
          title: barTitle
        })
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },
  /**
  * 显示报错信息
  */
  toastShow: function (message) {
    this.setData({
      message: message,
    })
  },
})